package view;

import service.impl.UserServiceImpl;
import util.InputUtil;

import java.util.List;

public class AdminView {
    private final UserServiceImpl userService;

    public AdminView(UserServiceImpl userService) {
        this.userService = userService;
    }

    public void start() {
        while (true) {
            System.out.println("\n=== 管理员菜单 ===");
            System.out.println("1. 查看所有用户");
            System.out.println("2. 添加用户");
            System.out.println("3. 删除用户");
            System.out.println("4. 修改用户信息");
            System.out.println("0. 退出登录");
            System.out.print("请选择：");
            switch (InputUtil.nextInt()) {
                case 1:
                    userService.findAll().forEach(System.out::println);
                    break;
                case 2:
                    System.out.print("用户名：");
                    String username = InputUtil.nextLine();
                    System.out.print("密码：");
                    String password = InputUtil.nextLine();
                    System.out.print("类型（经理/前台）：");
                    String type = InputUtil.nextLine();
                    System.out.print("手机号：");
                    String phone = InputUtil.nextLine();
                    if (userService.add(new entity.User(null, username, password, type, phone))) {
                        System.out.println("添加成功！");
                    } else {
                        System.out.println("用户名已存在！");
                    }
                    break;
                case 3:
                    System.out.print("输入用户ID删除：");
                    String id = InputUtil.nextLine();
                    if (userService.delete(id)) {
                        System.out.println("删除成功！");
                    } else {
                        System.out.println("未找到用户或无法删除admin！");
                    }
                    break;
                case 4:
                    System.out.print("输入用户ID修改：");
                    String uid = InputUtil.nextLine();
                    System.out.print("新类型（经理/前台）：");
                    String newType = InputUtil.nextLine();
                    System.out.print("新手机号：");
                    String newPhone = InputUtil.nextLine();
                    if (userService.updateType(uid, newType) && userService.updatePhone(uid, newPhone)) {
                        System.out.println("修改成功！");
                    } else {
                        System.out.println("未找到用户！");
                    }
                    break;
                case 0:
                    userService.shutdown();
                    return;
                default:
                    System.out.println("输入错误！");
            }
        }
    }
}